package cn.edu.sysu.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import cn.edu.sysu.dao.SolutionDao;
import cn.edu.sysu.dto.SolutionDto;
import cn.edu.sysu.util.Transaction;
import cn.edu.sysu.util.TransactionRead;

public class SolutionService {

	SolutionDao sDao = SolutionDao.INSTANCE;

	Transaction read = TransactionRead.INSTANCE;

	public Map<Integer, SolutionDto> getLatestSolutionByUser(int problem_id) {

		Map<Integer, SolutionDto> map = sDao.getLatestSolution(read, problem_id);

		return map;
	}

	public Map<Integer, List<SolutionDto>> getAllSolutionsByUser(int problem_id) {

		List<SolutionDto> list = sDao.getAllSolutionsForProblem(read, problem_id);

		Map<Integer, List<SolutionDto>> map = new HashMap<Integer, List<SolutionDto>>();

		for (Iterator iterator = list.iterator(); iterator.hasNext();) {
			SolutionDto solutionDto = (SolutionDto)iterator.next();
			if (map.containsKey(solutionDto.getUser_id())) {
				map.get(solutionDto.getUser_id()).add(solutionDto);
			} else {
				List<SolutionDto> solutions = new ArrayList<SolutionDto>();
				solutions.add(solutionDto);
				map.put(solutionDto.getUser_id(), solutions);
			}
		}
		return map;
	}

	public SolutionDto getSolution(int problemId, int userId, int revision) {
		return sDao.getSolution(read, problemId, userId, revision);
	}

	public List<SolutionDto> getSubmittedSolution(int problemId) {
		return sDao.getSubmittedSolution(read, problemId);
	}
}
